var tbody = document.querySelector('tbody')
function apply() {
    tbody.innerHTML = ''
    var xhr = new XMLHttpRequest()
    xhr.open('get', 'https://liu.zzgoodqc.cn/goodsx/goodslist')
    xhr.send()
    xhr.responseType = 'json'
    xhr.onload = () => {
        var a = xhr.response.data
        a.forEach((item) => {
            tbody.innerHTML += `
            <tr>
                 <td>${item.id}</td>
                 <td>${item.goods_name}</td>
                 <td>${item.stock}</td>
                 <td>${item.price}</td>
                 <td>${item.status == 1 ? '上架' : item.status == 2 ? '下架' : '其他'}</td>
                 <td>${item.sold}</td>
                  <td><img src="${item.img}"></td>
                 <td>
                <button class="layui-btn layui-btn-primary layui-border-green" onclick="del(${item.id})">删除</button><br>
                <button class="layui-btn layui-btn-primary layui-border-green" onclick="xg(${item.id} )">修改</button><br>
                <button class="layui-btn layui-btn-primary layui-border-green" onclick="xq(${item.id})">详情</button><br>
                <button class="layui-btn layui-btn-primary layui-border-green" onclick="gx(${item.id},${item.status})">状态更新</button>
                 </td>
            </tr>
            `
        })
    }
    xhr.onerror = (e) => {
        console.log(e);

    }
}
apply()

// 添加框
var tian = document.querySelector('.tian')
function show_add() {
    tian.style.display = 'block'
}
// 确认添加
function add() {
    var p1 = document.querySelectorAll('.tian input')[0].value
    var p2 = document.querySelectorAll('.tian input')[1].value
    var p3 = document.querySelectorAll('.tian input')[2].value
    var p4 = document.querySelectorAll('.tian input')[3].value
    var p5 = document.querySelectorAll('.tian input')[4].value
    var p6 = document.querySelectorAll('.tian input')[5].value

    var xhr = new XMLHttpRequest()
    xhr.open('post', 'https://liu.zzgoodqc.cn/goodsx/add')
    xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
    var obj = {
        goods_name: p1,
        stock: p2,
        price: p3,
        status: p4,
        sold: p5,
        img: p6,
    }
    xhr.send(JSON.stringify(obj))
    xhr.onload = () => {
        console.log(JSON.parse(xhr.response));
        apply()
        tian.style.display = 'none'
    }
    xhr.onerror = (e) => {
        console.log(e);

    }
}

// 删除
function del(id) {
    var xhr = new XMLHttpRequest()
    xhr.open('get', 'https://liu.zzgoodqc.cn/goodsx/delete?id=' + id)
    xhr.send()
    xhr.onload = () => {
        apply()
    }
    xhr.onerror = (e) => {
        console.log(e);

    }
}


// 修改
var iid
function xg(id) {
    iid = id
    document.querySelector('.xig').style.display = 'block'
}
function qr() {
    var q1 = document.querySelectorAll('.xig input')[0].value
    var q2 = document.querySelectorAll('.xig input')[1].value
    var q3 = document.querySelectorAll('.xig input')[2].value
    var q4 = document.querySelectorAll('.xig input')[3].value
    var q5 = document.querySelectorAll('.xig input')[4].value
    var q6 = document.querySelectorAll('.xig input')[5].value
    var xhr = new XMLHttpRequest()
    xhr.open('post', 'https://liu.zzgoodqc.cn/goodsx/update?id=' + iid)
    xhr.setRequestHeader('Content-Type', 'application/json;charset = utf-8')
    var obj = {
        goods_name: q1,
        stock: q2,
        price: q3,
        status: q4,
        sold: q5,
        img: q6,
    }

    xhr.send(JSON.stringify(obj))
    xhr.onload = () => {
        document.querySelector('.xig').style.display = 'none'
        apply()
    }
    xhr.onerror = (e) => {
        console.log(e);

    }

}

// 状态更新
function gx(id, pl) {
    if (pl == 1 && '上架') {
        pl = 2
    } else if (pl == 2 && '下架') {
        pl = 1
    }
    var xhr = new XMLHttpRequest()
    xhr.open('get', `https://liu.zzgoodqc.cn/goodsx/updateStatus?id=${id}&status=${pl}`)
    xhr.send()
    xhr.responseType = 'json'
    xhr.onload = () => {
        apply()
    }
    xhr.onerror = (e) => {
        console.log(e);

    }
}

function res() {
    var sou = document.querySelector('.so').value
    var xhr = new XMLHttpRequest()
    xhr.open('POST', 'https://liu.zzgoodqc.cn/goodsx/search?goods_name=' + sou)
    xhr.send()
    xhr.onload = () => {
        var jb = JSON.parse(xhr.response).data
        console.log(jb);
        document.querySelector('tbody').innerHTML = ''
        jb.forEach(item => {
            document.querySelector('tbody').innerHTML += `
                  <tr>
                  <td>${item.id}</td>
                  <td>${item.goods_name}</td>
                  <td>${item.stock}</td>
                  <td>${item.price}</td>
                   <td>${item.status == 1 ? '上架' : item.status == 2 ? '下架' : '其他'}</td>
                  <td>${item.sold}</td>
              <td><img src="${item.img}"></td>
                  <td>
                     <button onclick="del(${item.id})">删除</button>
                     <button onclick="xg(${item.id})">修改</button>
                     <button onclick="xq(${item.id})">详情</button>
                      <button onclick="gx(${item.id},${item.status})">状态更新</button>
                  </td>
                  </tr>
                  `
        })
    }
    xhr.onerror = (d) => {
        console.log(d);
    }
}

// 退出登录
function off() {
    location.assign('../登录/登录.html')
}
// 注销账户
function zx() {
    localStorage.clear()
    location.assign('../注册/注册.html')
}

//获取id进行跳转
function xq(id) {
    location.assign("../详情/详情.html?id=" + id)
}